﻿Imports System.Data.SqlClient

Public Class frmDiasOperacion

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub frmDiasOperacion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        lstDia.SelectedItem = "Seleccione un dia..."

        lblStatus.Text = "Listo"
    End Sub
    Sub CargaPlanes()
        dgPlan.Rows.Clear()

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim TipoDia As String = lstDia.Text
        Dim intTipoDia As Integer = 0
        Select Case TipoDia
            Case "Hábil"
                intTipoDia = 1
            Case "Sábado"
                intTipoDia = 2
            Case "Domingo"
                intTipoDia = 3
            Case "Vacaciones"
                intTipoDia = 4
            Case "Especial"
                intTipoDia = 5
        End Select

        Dim strSql As String = ""
        strSql = "SELECT IdPlan,Nombre,Despachos,Vueltas,Autobuses FROM PlanOperacion P INNER JOIN Rutas R ON P.IdRuta=R.IdRuta AND TipoDia=" & intTipoDia & " ORDER BY IDPLAN"


        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader
        Do While rdBuscar.Read()
            dgPlan.Rows.Add(0, rdBuscar("Nombre"), rdBuscar("Vueltas"), rdBuscar("Autobuses"))
        Loop

        dgPlan.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgPlan.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

    End Sub

    Sub CargaPlan()
        dgPlan.Rows.Clear()

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Fecha As String = datFecha.Value.ToString("MM/dd/yyyy")

        Dim strSql As String = ""
        strSql = "SELECT IdDia,Nombre,Vueltas,Autobuses FROM DiasOperacion P INNER JOIN Rutas R ON P.IdRuta=R.IdRuta AND CONVERT(VARCHAR,FechaOperacion,101)='" & Fecha & "'  ORDER BY IDDIA"

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Dim Cont As Integer = 0

        Do While rdBuscar.Read()
            dgPlan.Rows.Add(rdBuscar("IdDia"), rdBuscar("Nombre"), rdBuscar("Vueltas"), rdBuscar("Autobuses"))
            dgPlan.Rows(Cont).ReadOnly = True
            Cont = Cont + 1
        Loop

        dgPlan.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgPlan.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight


    End Sub

    Private Sub datFecha_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles datFecha.ValueChanged
        Dim Fecha As String = datFecha.Value.ToString("MM/dd/yyyy")
        Dim Registros As Integer = BuscarIntDatoCitra("SELECT COUNT(*) FROM DiasOperacion WHERE CONVERT(VARCHAR,FechaOperacion,101)='" & Fecha & "'")
        If Registros > 0 Then
            CargaPlan()
            lblStatus.Text = "El dia ya esta guardado"
        Else
            lblStatus.Text = "Listo para guardar"
        End If

    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Dim Fecha As String = datFecha.Value.ToString("MM/dd/yyyy")
        Call SetSqlCitra("DELETE FROM DiasOperacion WHERE CONVERT(VARCHAR,FechaOperacion,101)='" & Fecha & "'")
        CargaPlan()

    End Sub
    Sub Guardar()
        Dim Plan As New clsDiasOperacion
        Dim Dia As String = lstDia.Text
        Dim aux As Integer = 0

        If Dia = "" Then
            MsgBox("Debes de indicar el tipo de dia", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        For i = 0 To dgPlan.Rows.Count - 1
            Plan.FechaOperacion = datFecha.Value
            Plan.IdRuta = BuscarStrDatoCitra("SELECT IdRuta FROM Rutas WHERE Nombre='" & dgPlan.Rows(i).Cells(1).Value & "'")
            Select Case Dia
                Case "Hábil"
                    Plan.TipoDia = 1
                Case "Sábado"
                    Plan.TipoDia = 2
                Case "Domingo"
                    Plan.TipoDia = 3
                Case "Vacaciones"
                    Plan.TipoDia = 4
                Case "Especial"
                    Plan.TipoDia = 5
            End Select
            Plan.Vueltas = dgPlan.Rows(i).Cells(2).Value
            Plan.Autobuses = dgPlan.Rows(i).Cells(3).Value
            Plan.IdNomina = 0
            Plan.StatusNomina = 0
            Plan.Usuario = UsuarioActivo
            Plan.FechaUltimoCambio = Now()

            aux = dgPlan.Rows(i).Cells(0).Value
            If aux = 0 Then
                Plan.Insertar()
            End If
        Next
    End Sub

    Private Sub lstDia_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDia.SelectedIndexChanged
        CargaPlanes()

    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Try
            Guardar()
            dgPlan.Rows.Clear()
            datFecha.Value = Now()
            lblStatus.Text = "Listo"
            lstDia.SelectedItem = "Seleccione un dia..."

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        End Try
        MsgBox("Registros Guardados", MsgBoxStyle.Information, "Aviso")
    End Sub
End Class